﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLNS.Models;
using System.Text.RegularExpressions;
namespace QLNS.Controllers
{
    public class SearchController : Controller
    {
        //
        // GET: /Search/

        public ActionResult Index()
        {
            List<ChuyenMon> dschuyenmon = ChuyenMonModels.GetDSChuyenMon();
            ViewBag.DSChuyenMon = dschuyenmon;

            List<LyLuan> dslyluan = LyLuanModels.GetDSLyLuan();
            ViewBag.DSLyLuan = dslyluan;

            List<PhongBan> dsphongban = PhongBanModels.GetDSPhongBan(0);
            ViewBag.DsPhongBan = dsphongban;

            List<BangMaNgach> dsmangach = MaNgachModels.GetMaNgachTheoNhomNgach();
            ViewBag.DSMaNgach = dsmangach;

            List<QLNhaNuoc> dsqlnhanuoc = QLNNModels.GetDSQLNN();
            ViewBag.DSQLNN = dsqlnhanuoc;
            return View();
        }
        [HttpPost]
        public JsonResult GetSearchPaging(int pagenum, int pagesize, string sortdatafield, string sortorder, string id)
        {
            ITAPMNS2013Entities context = new ITAPMNS2013Entities();
            string[] timkiem = id.Split(',');
            var dbResult = (from cb in context.GetSearch()
                            select new {cb.HoTenCanBo,cb.IDCanBo,cb.HoTenKhongDau,cb.IDParent,cb.IDPhongBan,cb.MaNgachCC,cb.NgaySinh,cb.TenNgachCC,cb.TrinhDoChuyenMon
                            ,cb.TrinhDoLyLuan,cb.TrinhDoQLNN,cb.BacLuong,cb.HeSoLuong,cb.TenChucVu}
                           ).ToList();
            if (timkiem[1] != "")
            {
                var dbResult1 = (from cb in context.GetSearch()
                                 select new
                                 {
                                     cb.HoTenCanBo,
                                     cb.IDCanBo,
                                     cb.HoTenKhongDau,
                                     cb.IDParent,
                                     cb.IDPhongBan,
                                     cb.MaNgachCC,
                                     cb.NgaySinh,
                                     cb.TenNgachCC,
                                     cb.TrinhDoChuyenMon,
                                     cb.TrinhDoLyLuan,
                                     cb.TrinhDoQLNN,
                                     cb.BacLuong,
                                     cb.HeSoLuong,
                                     cb.TenChucVu
                                 }
                             );
                var str = timkiem[1];
                dbResult1 = dbResult1.Where(c => c.HoTenCanBo.Contains(str) || c.HoTenKhongDau.Contains(str));
                if (timkiem[0] != "1")
                {
                    dbResult1 = dbResult1.Where(c => c.IDPhongBan == int.Parse(timkiem[0]) || c.IDParent == int.Parse(timkiem[0]));
                }
                if (timkiem[2] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.MaNgachCC.Contains(timkiem[2]));
                }
                if (timkiem[3] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoChuyenMon == int.Parse(timkiem[3]));
                }
                if (timkiem[4] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoLyLuan == int.Parse(timkiem[4]));
                }
                if (timkiem[5] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoQLNN == int.Parse(timkiem[5]));
                }
                dbResult = dbResult1.ToList();
            }
            else
            {
                var dbResult1 = (from cb in context.GetSearch()
                                 select new
                                 {
                                     cb.HoTenCanBo,
                                     cb.IDCanBo,
                                     cb.HoTenKhongDau,
                                     cb.IDParent,
                                     cb.IDPhongBan,
                                     cb.MaNgachCC,
                                     cb.NgaySinh,
                                     cb.TenNgachCC,
                                     cb.TrinhDoChuyenMon,
                                     cb.TrinhDoLyLuan,
                                     cb.TrinhDoQLNN,
                                     cb.BacLuong,
                                     cb.HeSoLuong,
                                     cb.TenChucVu
                                 }
                             );
                if (timkiem[0] != "1")
                {
                    dbResult1 = dbResult1.Where(c => c.IDPhongBan == int.Parse(timkiem[0]) || c.IDParent == int.Parse(timkiem[0]));
                }
                if (timkiem[2] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.MaNgachCC.Contains(timkiem[2]));
                }
                if (timkiem[3] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoChuyenMon == int.Parse(timkiem[3]));
                }
                if (timkiem[4] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoLyLuan == int.Parse(timkiem[4]));
                }
                if (timkiem[5] != "0")
                {
                    dbResult1 = dbResult1.Where(c => c.TrinhDoQLNN == int.Parse(timkiem[5]));
                }
                dbResult = dbResult1.ToList();
            }
            var customers = (from customer in dbResult
                             select new
                             {
                                 customer.IDCanBo,                                 
                                 customer.HoTenCanBo,
                                 customer.NgaySinh,
                                 customer.MaNgachCC,
                                 customer.TenNgachCC,
                                 customer.TenChucVu,
                                 customer.BacLuong,
                                 customer.HeSoLuong
                             });
            if (sortorder != null)
            {
                if (sortorder == "asc")
                {
                    customers = customers.OrderBy(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
                else if (sortorder == "desc")
                {
                    customers = customers.OrderByDescending(c => c.GetType().GetProperty(sortdatafield).GetValue(c, null));
                }
            }
            customers = customers.Skip(pagenum * pagesize).Take(pagesize);
            var result = new
            {
                TotalRows = dbResult.Count(),
                Rows = customers
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }

      
    }
}
